package com.sinbad.produce.controller;

import com.sinbad.produce.utils.HtmlParseUtils;
import com.sinbad.produce.vo.Content;
import com.sinbad.produce.vo.KnowParticular;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * @author chengzx
 * date 2021/8/13 11:41
 */
@Api("网页数据爬取")
@RestController
@RequestMapping("content")
public class ContentController {

    @Autowired
    private HtmlParseUtils htmlParseUtils;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 爬取数据
     * @param keyWords
     * @return
     */
    @GetMapping("/parseContent")
    public  List<Content> parseContent(String keyWords) throws IOException {
        List<Content> contents = htmlParseUtils.listGoods(keyWords);
        return contents;
    }

    @ApiOperation(value = "爬取【急诊知识】数据")
    @GetMapping("/parseKnowParticular")
    public List<KnowParticular> parseKnowParticular() throws IOException {
        return htmlParseUtils.getKnowParticular();
    }

    @ApiOperation(value = "爬取【急诊知识】数据保存到redis")
    @GetMapping("/saveKnowParticular")
    public boolean saveKnowParticular() throws IOException {
        List<KnowParticular> knowParticular = htmlParseUtils.getKnowParticular();
        ListOperations opsForList = redisTemplate.opsForList();
        opsForList.leftPushAll("knowParticular",knowParticular);
        redisTemplate.expire("knowParticular",1, TimeUnit.DAYS);
        return true;
    }

}
